import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-13
 * Time:20:26
 */
class TcpClient{
    private Socket socket=null;
    public  TcpClient(String severIp,int severPort) throws IOException {
        socket =new Socket(severIp,severPort);
    }
    public void start (){
        Scanner scanner =new Scanner(System.in);
        try(InputStream inputStream= socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream()) {
            while(true){
                System.out.println("请输入：->");
                String request=scanner.next();
                //建立联系，发送数据
                PrintWriter printWriter=new PrintWriter(outputStream);
                printWriter.println(request);
                //刷新缓冲区
                printWriter.flush();
                //读取响应
                Scanner respScanner=new Scanner(inputStream);
                String response=respScanner.next();
                //响应显示到界面上
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
public class TcpClientTest {
    public static void main(String[] args) throws IOException {
        TcpClient tcpClient=new TcpClient("127.0.0.1",9090);
        tcpClient.start();
    }
}
